package com.litong.rms.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import com.litong.rms.bean.Auth;
import com.litong.rms.bean.Function;
import com.litong.rms.bean.Module;
import com.litong.rms.bean.Role;
import com.litong.rms.bean.RoleAuth;
import com.litong.rms.bean.RoleRelate;
import com.litong.rms.service.AuthService;
import com.litong.rms.service.FunctionService;
import com.litong.rms.service.ModuleService;
import com.litong.rms.service.RoleAuthService;
import com.litong.rms.service.RoleRelateService;
import com.litong.rms.service.RoleService;

public class RoleAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Role role;//角色实体
	
	private Auth auth;//权限实体
	
	private RoleAuth roleAuth;//角色权限实体
	
	private RoleRelate roleRelate;//角色继承关系实体
	
	private RoleService roleService;//角色服务
	
	private String roleId;//角色ID
	
	private List<Role> roleList;//角色列表
	
	private List<Auth> authList;//权限列表
	
	private ModuleService moduleService;//模块服务
	
	private FunctionService functionService;//功能服务
	
	private RoleAuthService roleAuthService;//角色权限服务
	
	private AuthService authService;//权限服务
	
	private RoleRelateService roleRelateService;
	
	private List<Module> moduleList;//模块列表
	
	private List<Function> functionList;//功能列表
	
	private List<String> lstTree;//构造存放树list
	
	private JSONArray dataArray ;//将lstTree转换成json格式的实体
	
	private String nodeSelect;
	
	private int[] selectedAuthId;//已经赋予的权限ID
	
	private List<RoleAuth> roleAuthList;
	
	private List<RoleRelate> roleRelateList;
	
	private int[] selectedRoleRealteId;//已经继承的父类Id

	private String[] proleIdArray;//选中继承的父id
	
	
	/*获取角色列表*/
	public String list(){
		roleList = roleService.getRoleList();
		return SUCCESS;
	}
	
	/*生成权限角色关系树*/
	public String buildTree(){
		authList = authService.getAuthList();
		roleList = roleService.getRoleList();
		moduleList = moduleService.getModuleList();
		functionList = functionService.getFunctionList();
		Map<Integer,String> moduMap = new HashMap<Integer,String>();
		for(int i = 0;i<moduleList.size();i++){
			moduMap.put(moduleList.get(i).getModuleId(),moduleList.get(i).getModuleName());
		}
		Map<Integer,String> funcMap = new HashMap<Integer,String>();
		for(int i = 0;i<functionList.size();i++){
			funcMap.put(functionList.get(i).getFunctionId(),functionList.get(i).getFunctionName());
		}
		String str = null;
		lstTree = new ArrayList<String>(); 
		for(int i = 0;i<roleList.size();i++){
			role = roleList.get(i);
			str = "{id:"+role.getRoleId()+", pId:0, name:\""+role.getRoleName()+"\",open:false}";
			lstTree.add(str);
			roleAuthList = roleAuthService.getRoleAuthList(role.getRoleId());
			this.selectedAuth(roleAuthList);
			boolean b;
			for(int j = 0;j<authList.size();j++){
				b = false;
				auth = authList.get(j);
				for(int n = 0;n<selectedAuthId.length;n++){
					if(selectedAuthId[n]==auth.getAuthId()){
						b = true;
						n = selectedAuthId.length;
					}
				}
				str =  "{id:"+auth.getAuthId()+"abc"+", pId:"+role.getRoleId()+", name:\"【"+moduMap.get(auth.getModuleId())+"】的【#"+funcMap.get(auth.getFunctionId())+"#】权限"+"\",checked:"+b+",open:true}";
				lstTree.add(str);
			}
		}
		dataArray = JSONArray.fromObject(lstTree);
		return SUCCESS;
	}
	
	/*删除角色*/
	public String deleteRole(){
		//删除继承该角色的子角色权限
		roleAuthList = roleAuthService.getRoleAuthList(Integer.parseInt(roleId));
		roleRelateList = roleRelateService.getRoleRelateListByf(Integer.parseInt(roleId));
		RoleAuth ra = new RoleAuth();
		roleAuth = new RoleAuth();
		if(roleRelateList!=null&&!roleRelateList.isEmpty()){
			for(int i = 0;i<roleRelateList.size();i++){
				for(int j = 0;j<roleAuthList.size();j++){
					roleAuth.setAuthId(roleAuthList.get(j).getAuthId());
					roleAuth.setRoleId(roleRelateList.get(i).getsRoleId());
					ra = roleAuthService.findRoleAuth(roleAuth);
					if(ra!=null){
						roleAuthService.deleteRoleAuth(ra);
					}
				}
			}
		}
		//删除角色的继承关系
		roleRelateService.deleteRoleRelate("sRoleId", Integer.parseInt(roleId));
		roleRelateService.deleteRoleRelate("fRoleId", Integer.parseInt(roleId));
		//删除角色分配的权限
		roleAuthService.deleteRoleAuthByRoleId(Integer.parseInt(roleId));
		
		//删除角色
		role = new Role();
		role.setRoleId(Integer.parseInt(roleId));
		roleService.deleteRole(role);
		
		return list();
	}
	/*添加角色*/
	public String addRole(){
		roleService.addRole(role);
		roleRelate = new RoleRelate();
		//将继承关系存储如数据
		if(proleIdArray.length>0&&proleIdArray!=null){
			for(int i=0;i<proleIdArray.length;i++){
				roleRelate.setfRoleId(Integer.parseInt(proleIdArray[i]));
				roleRelate.setsRoleId(role.getRoleId());
				roleRelateService.addRoleRelate(roleRelate);
				//将父类的权限分配各子类
				roleAuthList = roleAuthService.getRoleAuthList(Integer.parseInt(proleIdArray[i]));//将继承的父ID传入
				this.selectedAuth(roleAuthList);
				
				for(int j=0;j<selectedAuthId.length;j++){
					roleAuth.setRoleId(role.getRoleId());
					roleAuth.setAuthId(selectedAuthId[j]);
					roleAuthService.addRoleAuth(roleAuth);
				}
				
			}
			
		}
		return SUCCESS;
	}
	
	
	/*根据ID查询角色*/
	public String findUpdateRole(){
		roleList = roleService.getRoleList();
		role = roleService.findById(Integer.parseInt(roleId));
		roleRelateList = roleRelateService.getRoleRelateList(Integer.parseInt(roleId));
		this.selectedRoleRelate(roleRelateList);
		return SUCCESS;
	}
	
	
	/*更新角色*/
	public String updateRole(){
		roleService.updateRole(role);
		//删除子类继承父类的所有权限
		
		//首先查出子类继承的父类Id
		roleRelateList = roleRelateService.getRoleRelateList(role.getRoleId());
		this.selectedRoleRelate(roleRelateList);
		for(int i=0;i<selectedRoleRealteId.length;i++){
			//根据父类Id查找出父类的分配的权限Id
			roleAuthList = roleAuthService.getRoleAuthList(selectedRoleRealteId[i]);
			this.selectedAuth(roleAuthList);
			//删除子类继承父类的权限
			for(int j=0;j<selectedAuthId.length;j++){
				roleAuth.setAuthId(selectedAuthId[j]);//传入父类继承的权限Id
				roleAuth.setRoleId(role.getRoleId());//传入子类角色Id
				RoleAuth rA = roleAuthService.findRoleAuth(roleAuth);//查看表中是否存在roleId和AuthId与传入参数匹配的实体。
				if(rA!=null){//如果有删除
					roleAuthService.deleteRoleAuth(rA);
				}
			}
			
		}
		//删除角色关系表的子类和父类继承关系。
		roleRelateService.deleteRoleRelate("sRoleId", role.getRoleId());
		//将更改的权限分配给角色。
		//将继承关系存入数据库
		roleRelate = new RoleRelate();
		if(proleIdArray.length>0&&proleIdArray!=null){
			for(int i=0;i<proleIdArray.length;i++){
				roleRelate.setfRoleId(Integer.parseInt(proleIdArray[i]));
				roleRelate.setsRoleId(role.getRoleId());
				roleRelateService.addRoleRelate(roleRelate);
				//将父类的权限分配各子类
				roleAuthList = roleAuthService.getRoleAuthList(Integer.parseInt(proleIdArray[i]));//将继承的父ID传入
				this.selectedAuth(roleAuthList);
				
				for(int j=0;j<selectedAuthId.length;j++){
					roleAuth.setRoleId(role.getRoleId());
					roleAuth.setAuthId(selectedAuthId[j]);
					roleAuthService.addRoleAuth(roleAuth);
				}
				
			}
			
		}
		return	SUCCESS;
	}
	
	
	//更新角色的权限分配
	public String updateByTree(){
		String[] str1 = nodeSelect.split(",");
		String s1,s2;
		roleAuth = new RoleAuth();
		for(int i = 0;i<str1.length;i++){
			String[] str2 = str1[i].split(":");
			
			if(str2[0].length()>3){
				s1 = str2[0].substring(0, str2[0].length()-3).trim();
				s2 = str2[1].trim();
				
				
				//如果粗在继承的子类还要更改其子类的权限
				List<RoleRelate> roleRelateListf = roleRelateService.getRoleRelateListByf(Integer.parseInt(s2));
				RoleRelate roleRelate = new RoleRelate();
				if(roleRelateListf!=null){
					for(int j = 0;j<roleRelateListf.size();j++){
						roleRelate = roleRelateListf.get(j);
						roleAuth.setAuthId(Integer.parseInt(s1));
						roleAuth.setRoleId(roleRelate.getsRoleId());
						RoleAuth rA = roleAuthService.findRoleAuth(roleAuth);
						if(rA==null){
							
							roleAuthService.addRoleAuth(roleAuth);
						}else{
							roleAuthService.deleteRoleAuth(rA);
						}
					}
				}
				
				
				roleAuth.setAuthId(Integer.parseInt(s1));
				roleAuth.setRoleId(Integer.parseInt(s2));
				RoleAuth rA = roleAuthService.findRoleAuth(roleAuth);
				if(rA==null){
	
					roleAuthService.addRoleAuth(roleAuth);
				}else{
					roleAuthService.deleteRoleAuth(rA);
				}
			}
			
		}
		//System.out.println(str1.toString());
		return SUCCESS;
		
	}
	
	//得到分配权限的ID
	@SuppressWarnings("null")
	public void selectedAuth(List<RoleAuth> roleAuthList){
		
		if(roleAuthList!=null||!roleAuthList.isEmpty()){
			selectedAuthId = new int[roleAuthList.size()];
			for(int i=0;i<roleAuthList.size();i++){
				roleAuth = roleAuthList.get(i);
				selectedAuthId[i] = roleAuth.getAuthId();
			}
		}
		
	}
	
	//得到继承的父ID
	@SuppressWarnings("null")
	public void selectedRoleRelate(List<RoleRelate> roleRelateList){
		if(roleRelateList!=null||!roleRelateList.isEmpty()){
			selectedRoleRealteId = new int[roleRelateList.size()];
			for(int i=0;i<roleRelateList.size();i++){
				roleRelate = roleRelateList.get(i);
				selectedRoleRealteId[i] = roleRelate.getfRoleId();
			}
		}
	} 
	
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	/*public RoleService getRoleService() {
		return roleService;
	}*/

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public String getRoleId() {
		return roleId;
	}

	public RoleRelate getRoleRelate() {
		return roleRelate;
	}

	public void setRoleRelate(RoleRelate roleRelate) {
		this.roleRelate = roleRelate;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}


	public void setAuthList(List<Auth> authList) {
		this.authList = authList;
	}


	public List<Auth> getAuthList() {
		return authList;
	}


	public void setAuthService(AuthService authService) {
		this.authService = authService;
	}


	public JSONArray getDataArray() {
		return dataArray;
	}

	public void setDataArray(JSONArray dataArray) {
		this.dataArray = dataArray;
	}

	public void setAuth(Auth auth) {
		this.auth = auth;
	}

	public Auth getAuth() {
		return auth;
	}

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	/*public ModuleService getModuleService() {
		return moduleService;
	}*/

	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}

	/*public FunctionService getFunctionService() {
		return functionService;
	}*/

	public void setModuleList(List<Module> moduleList) {
		this.moduleList = moduleList;
	}

	public List<Module> getModuleList() {
		return moduleList;
	}

	public void setFunctionList(List<Function> functionList) {
		this.functionList = functionList;
	}

	public List<Function> getFunctionList() {
		return functionList;
	}
	
	public String getNodeSelect() {
		return nodeSelect;
	}

	public void setNodeSelect(String nodeSelect) {
		this.nodeSelect = nodeSelect;
	}

	public void setRoleAuthService(RoleAuthService roleAuthService) {
		this.roleAuthService = roleAuthService;
	}

	public void setSelectedAuthId(int[] selectedAuthId) {
		this.selectedAuthId = selectedAuthId;
	}

	public int[] getSelectedAuthId() {
		return selectedAuthId;
	}

	public void setProleIdArray(String[] proleIdArray) {
		this.proleIdArray = proleIdArray;
	}

	public String[] getProleIdArray() {
		return proleIdArray;
	}

	public void setRoleRelateService(RoleRelateService roleRelateService) {
		this.roleRelateService = roleRelateService;
	}

	public void setRoleRelateList(List<RoleRelate> roleRelateList) {
		this.roleRelateList = roleRelateList;
	}

	public List<RoleRelate> getRoleRelateList() {
		return roleRelateList;
	}

	public void setSelectedRoleRealteId(int[] selectedRoleRealteId) {
		this.selectedRoleRealteId = selectedRoleRealteId;
	}

	public int[] getSelectedRoleRealteId() {
		return selectedRoleRealteId;
	}

	/*public RoleRelateService getRoleRelateService() {
		return roleRelateService;
	}*/

	/*public RoleAuthService getRoleAuthService() {
		return roleAuthService;
	}*/

	/*public AuthService getAuthService() {
		return authService;
	}*/

}
